package com.nativetalk.controller.app.two.course;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.comment.TdNtCommentCouse;
import com.nativetalk.bean.course.TdNtCoursePictureList;
import com.nativetalk.bean.course.TwoTsNtCourseInfoDetail;
import com.nativetalk.bean.course.catalog.TdNtCourseCatalog;
import com.nativetalk.bean.level.TdNtCourseLevelDetail;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.member.course.TdNtMembCourseDetail;
import com.nativetalk.bean.website.TsNtWebSiteConfIndex;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.service.member.course.search.MembCourseSearchService;
import com.nativetalk.service.teacher.search.TeacherSearchService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class TwoCourseController {
    private static Logger logger = Logger.getLogger(TwoCourseController.class);
    @Resource
    private TeacherSearchService teacherSearchService;
    @Resource
    private MembCourseSearchService membCourseSearchService;

    /**
     * @return RetInfo
     * @Purpose 查找老师
     * @version 2.1
     * @time 2015-12-25
     * @author lizhun
     */
    @RequestMapping(value = "/app/5/teachers/list", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo teacher(@RequestBody Page page,
                           @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            TdNtMember tdNtMember = null;
            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isNotBlank(token)) {
                Object o = MemcachedUtils.get(token);
                if (o != null && TdNtMember.class.isInstance(o)) {
                    tdNtMember = (TdNtMember) o;
                }
            }

            RetInfo retInfo = teacherSearchService.findTeacherListTwo(page, tdNtMember);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 教材信息
     * @version 2.1
     * @time 2015-12-25
     * @author lizhun
     */
    @RequestMapping(value = "/app/5/course/index", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo index(@RequestBody Map<String, String> mapParam,
                         @RequestHeader(value = "token", required = false) String token) {
        RetInfo retInfo = new RetInfo();
        Map<String, Object> map = new HashMap<>();
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(mapParam));

            Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
            map.put("free", tsNtWebSiteConfIndexMap.get("9").getData_value());
            map.put("free_start", tsNtWebSiteConfIndexMap.get("10").getData_value());
            map.put("free_end", tsNtWebSiteConfIndexMap.get("11").getData_value());
            map.put("share", tsNtWebSiteConfIndexMap.get("12").getData_value());
            map.put("share_name", tsNtWebSiteConfIndexMap.get("13").getData_value());
            map.put("share_url", tsNtWebSiteConfIndexMap.get("14").getData_value());
            map.put("share_picutre", tsNtWebSiteConfIndexMap.get("15").getData_value());
            map.put("share_content", tsNtWebSiteConfIndexMap.get("16").getData_value());

            Map<String, TwoTsNtCourseInfoDetail> tsNtCourseInfoDetailMapTwo = (Map<String, TwoTsNtCourseInfoDetail>) MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMapTwo);
            map.put("course", tsNtCourseInfoDetailMapTwo.get(mapParam.get("cou_id")));
            Map<String, List<TdNtCommentCouse>> tdNtCommentCouseMap = (Map<String, List<TdNtCommentCouse>>) MemcachedUtils.get(MemcachedKey.tdNtCommentCouseMap);

            Map<String, Map<String, List<TdNtCourseCatalog>>> tdNtCourseCatalogLevelMap = (Map<String, Map<String, List<TdNtCourseCatalog>>>) MemcachedUtils.get(MemcachedKey.tdNtCourseCatalogLevelMap);
            map.put("level", tdNtCourseCatalogLevelMap.get(mapParam.get("cou_id")));
            Map<String, List<TdNtCourseLevelDetail>> tdNtCourseLevelDetailsMap = (Map<String, List<TdNtCourseLevelDetail>>) MemcachedUtils.get(MemcachedKey.tdNtCourseLevelDetailsMap);
            map.put("levelDetail", tdNtCourseLevelDetailsMap.get(mapParam.get("cou_id")));
//          Map<String, TsNtSetmealIndex> tsNtSetmealSynopisMap = (Map<String, TsNtSetmealIndex>) MemcachedUtils.get(MemcachedKey.tsNtSetmealIndexMap);
//          String cate_id = tsNtCourseInfoDetailMapTwo.get(mapParam.get("cou_id")).getCate_id().toString();
//          if (tsNtSetmealSynopisMap.get(cate_id) != null) {
//            map.put("setmeal", tsNtSetmealSynopisMap.get(cate_id));
//          } else {
//            map.put("setmeal", tsNtSetmealSynopisMap.get("0"));
//          }


            /**
             * 查找前十条评论
             */
            List<TdNtCommentCouse> tdNtCommentCouses = tdNtCommentCouseMap.get(mapParam.get("cou_id"));
            int totalRecord;
            if (tdNtCommentCouses != null) {
                totalRecord = tdNtCommentCouses.size();//总条数
            } else {
                totalRecord = 0;//总条数
            }

            int totalPage;//总页数
            int pageSize = 10;
            int currentPage = 1;

            if (totalRecord % pageSize == 0) {
                totalPage = totalRecord / pageSize;
            } else {
                totalPage = (totalRecord / pageSize) + 1;
            }
            if (currentPage >= totalPage) {
                currentPage = totalPage;
            }
            if (tdNtCommentCouses == null) {
                map.put("comment", tdNtCommentCouses);
            } else if (tdNtCommentCouses.size() > 10) {
                map.put("comment", tdNtCommentCouses.subList(0, 10));
            } else {
                map.put("comment", tdNtCommentCouses);
            }
            map.put("currentPage", currentPage);
            map.put("totalPage", totalPage);
            map.put("totalRecord", totalRecord);

            Map<String, List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String, List<TdNtCoursePictureList>>) MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);
            List<TdNtCoursePictureList> coursePictureLists = tdNtCoursePictureListMap.get(mapParam.get("cou_id"));

            TdNtMember tdNtMember = null;
            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isNotBlank(token)) {
                Object o = MemcachedUtils.get(token);
                if (o != null && TdNtMember.class.isInstance(o)) {
                    tdNtMember = (TdNtMember) o;
                }
            }

            if (tdNtMember != null) {
                TdNtMembCourseDetail tdNtMembCourse = membCourseSearchService.findTdNtMembCourseDetail(tdNtMember.getMemb_id(), Integer.valueOf(mapParam.get("cou_id")));
                if (tdNtMembCourse != null) {
                    map.put("studySize", tdNtMembCourse.getStudy_page());
                    map.put("coursePictureSize", coursePictureLists.size());
                } else {
                    map.put("studySize", 0);
                    map.put("coursePictureSize", coursePictureLists.size());
                }
                map.put("share_url", tsNtWebSiteConfIndexMap.get("14").getData_value() + "?invite_code="
                        + tdNtMember.getMemb_invite_code());
            } else {
                map.put("studySize", 0);
                map.put("coursePictureSize", coursePictureLists.size());
            }
            if (coursePictureLists.size() >= 5) {
                map.put("coursePicture", coursePictureLists.subList(0, 5));
            } else {
                map.put("coursePicture", coursePictureLists);
            }

            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取教程数据成功");
            retInfo.setObj(map);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }

    }

}


